package top.bug.blog.controller.backend;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import top.bug.blog.annotation.Auth;
import top.bug.blog.common.Result;
import top.bug.blog.schedule.SchedulerManage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 定时任务 Controller
 */
@Api(tags = "定时任务模块")
@RestController
@RequestMapping("/jobApi")
@Auth
public class JobController {

    @Resource
    private SchedulerManage schedulerManage;

    /**
     * 创建定时任务
     *
     * @param requestMap 包含jobName, jobGroup, cronExpression等参数的Map
     */
    @PostMapping("createJob")
    public Result<Void> createJob(@RequestBody Map<String, Object> requestMap) {
        // 调用Service方法创建任务
        schedulerManage.scheduleJob(requestMap);
        return Result.success();
    }

    /**
     * 手动立即执行一次任务
     */
    @PostMapping("/trigger")
    public Result<Void> triggerJob(String jobName, String jobGroup) {
        schedulerManage.triggerJob(jobName, jobGroup);
        return Result.success();
    }

    /**
     * 暂停任务
     */
    @PostMapping("/pause")
    public Result<Void> pauseJob(String jobName, String jobGroup) {
        schedulerManage.pauseJob(jobName, jobGroup);
        return Result.success();
    }

    /**
     * 恢复任务
     */
    @PostMapping("/resume")
    public Result<Void> resumeJob(String jobName, String jobGroup) {
        schedulerManage.resumeJob(jobName, jobGroup);
        return Result.success();
    }

    /**
     * 删除任务
     */
    @PostMapping("/delete")
    public Result<Void> deleteJob(String jobName, String jobGroup) {
        schedulerManage.deleteJob(jobName, jobGroup);
        return Result.success();
    }

    /**
     * 更新任务的Cron表达式和参数
     *
     * @param jobName    任务名 (PathVariable)
     * @param jobGroup   任务组 (PathVariable)
     * @param requestMap 请求体，应包含newCronExpression和jobData(可选)
     */
    @PostMapping("/update")
    public Result<String> updateJob(String jobName, String jobGroup,
                                    @RequestBody Map<String, Object> requestMap) {
        // 调用Service方法
        Date nextFireTime = schedulerManage.updateJob(jobName, jobGroup, requestMap);
        String message = String.format("任务更新成功！下次执行时间: %s", nextFireTime);
        return Result.success(message);


    }

    /**
     * 获取所有任务的详细信息（包括状态、上次/下次执行时间等）[3](@ref)
     */
    @GetMapping("/getAllJobs")
    public Result<List<Map<String, Object>>> getAllJobs() {
        List<Map<String, Object>> jobs = schedulerManage.getAllJobDetails();
        return Result.success(jobs);
    }

    /**
     * 获取所有任务的详细信息（包括状态、上次/下次执行时间等）[3](@ref)
     */
    @PostMapping("/getNextExecutionTimes")
    public Result<List<String>> getNextExecutionTimes(@RequestBody Map<String, Object> requestMap) {
        String jobCron = String.valueOf(requestMap.getOrDefault("jobCron", ""));
        List<String> nextTimes = schedulerManage.getNextExecutionTimes(jobCron, 5);
        return Result.success(nextTimes);
    }
}